import Vue from 'vue'
import VueRouter from 'vue-router'

//把原来的的路由模式转换成路由懒加载的模式

import login from '../views/login'
//const login = () => import(/* webpackChunkName: "login_Home_Welcome" */ '../views/login.vue')
import Home from '../views/Home'
//const Home = () => import(/* webpackChunkName: "login_Home_Welcome" */ '../views/Home.vue')
import Welcome from '../views/Welcome'
//const Welcome = () => import(/* webpackChunkName: "login_Home_Welcome" */ '../views/Welcome.vue')

import user from '../views/users/users'
//const user= () => import(/* webpackChunkName: "user_roles_rights" */ '../views/users/users')
import roles from '../views/power/roles'
//const roles = () => import(/* webpackChunkName: "user_roles_rights" */ '../views/power/roles')
import rights from '../views/power/rights'
//const rights = () => import(/* webpackChunkName: "user_roles_rights" */ '../views/power/rights')


import categories from '../views/goods/categories'
//const  categories = () => import(/* webpackChunkName: "categories_params" */ '../views/goods/categories')
import params from '../views/goods/params'
//const  params = () => import(/* webpackChunkName: "categories_params" */ '../views/goods/params')

import goodsList from '../views/goods/list'
//const  goodsList  = () => import(/* webpackChunkName: "goodsList_add " */ '../views/goods/list')
import add from '../views/goods/add'
//const  add  = () => import(/* webpackChunkName: "goodsList_add" */ '../views/goods/add')

import order from '../views/order/orders'
//const  order  = () => import(/* webpackChunkName: "order_report" */ '../views/order/orders')
import report from '../views/report/report'
//const  report = () => import(/* webpackChunkName: "order_report" */ '../views/report/report')

// 配置路由
const router = new VueRouter({
  mode: 'history',
  base:'/',
  routes: [
    {
      // 路由路径
      path: '/login',
      // 路由名称
      name: 'login',
      // 跳转到组件
      component: login
    },
    // 路由重定向
    { path: '/', redirect: login },
    {
      // 路由路径
      path: '/Home',
      // 路由名称
      name: 'Home',
      // 跳转到组件
      component: Home,
      redirect: {name: 'Welcome'},
      children: [
        { path: '/Welcome', name: 'Welcome', component: Welcome },
        { path: '/users', name: 'users', component: user },
        { path: '/roles', name: 'roles', component: roles },
        { path: '/rights', name: 'rights', component: rights },
        { path: '/categories', name: 'categories', component: categories },
        { path: '/params', component: params },
        { path: '/goods', component: goodsList },
        { path: '/goods/add', component: add },
        { path: '/orders', component: order },
        { path: '/reports', component: report }

      ]
    },
    
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数
  // next（）表示放行  next（'/login'） 表示强行跳转到login
  if (to.path === '/login') return next()
  // 获取token值
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
